<?php

namespace app\index\controller\rush;

use app\index\service\ConsultingRushService;
use ShidianUtil\Thinkphp\Response\ApiResponse;
use ShidianUtil\Utils\CheckUtil;
use app\index\model\UM as UM;
use app\index\service\ConsultingService;
use think\APP;
use think\exception\ValidateException;
use think\Request as Request;

/**
 * Consulting 控制器
 * Class Consulting
 * @package app\index\controller\rush;
 * @link https://www.kancloud.cn/manual/thinkphp6_0/1037509
 * @see \app\index\validate\rush\Consulting
 */
class Consulting
{
    // 请求参数
    private Request $request;

    // service
    private ConsultingRushService $service;

    public function __construct(Request $request, ConsultingRushService $service)
    {
        $this->request = $request;
        $this->service = $service;
    }

    /**
     * 咨询赚钱-列表
     * @param UM|null $user
     * @return ApiResponse
     * @url /rush/consulting/index
     */
    public function index(?UM $user): ApiResponse
    {
        $request     = $this->request;

        $listType = $request->param('listType','0');
        $page      = $request->param('page', 1);
        $page_size = $request->param('page_size', 20);

        return ApiResponse::success($this->service->indexv2($user, $listType, $page, $page_size));
    }

    /**
     * 抢单+确认
     * @param UM $user
     * @return ApiResponse
     * @url /rush/consulting/save
     */
    public function save(UM $user): ApiResponse
    {
        $request     = $this->request;

        $id = $request->param('id');
        if (!is_id($id)) {
            throw new ValidateException('请选择一个咨询');
        }

        $data = $this->service->rushAndConfirm($user, $request->param('id'), $request->param());

        return ApiResponse::success($data);
    }

}
